package com.example.yuejian.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.example.yuejian.R;
import com.example.yuejian.adapter.ClubAdapter;
import com.example.yuejian.adapter.TrainAdapter;
import com.example.yuejian.bean.ClubEntity;
import com.example.yuejian.bean.TrainEntity;
import com.example.yuejian.common.GlideImageLoader;
import com.example.yuejian.common.Url;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ClubActivity extends BaseActivity {



    @BindView(R.id.recy)
    RecyclerView recyclerView;


    RecyclerView.Adapter adapter;

    LinearLayoutManager layoutManager;
    private List<ClubEntity> entityList;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_club);

        super.onCreate(savedInstanceState);

        context = getBaseContext();
        initView();

        all();

    }

    private void initView() {

        entityList = new ArrayList<>();

        adapter = new ClubAdapter(context, entityList, R.layout.club_item);

        layoutManager = new LinearLayoutManager(context);

        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
//设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
//设置Adapter
        recyclerView.setAdapter(adapter);
//设置增加或删除条目的动画
        recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }


    Handler handler_activity = new mHandler();
    @OnClick(R.id.zuijing)
    void zuijing() {

        EasyHttp.get("yuejian6/public/index.php/json/club/distanceclub")
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });
    }
    @OnClick(R.id.all)
    void all(){
        EasyHttp.get("yuejian6/public/index.php/json/club/lists")
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });
    }

    @OnClick(R.id.renqi)
    void renqi() {

        EasyHttp.get("yuejian6/public/index.php/json/club/clubhot")
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });
    }

    private static final String TAG = "TrainActivity";
    class mHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case 0:
                    entityList.clear();

                    List<ClubEntity> entities = null;

                    try{
                        entities = JSON.parseArray(msg.obj.toString(),ClubEntity.class);
                    }catch (Exception e){

                    }

                    if (entities==null){
                        return;
                    }

                    entityList.addAll(entities);

                    adapter.notifyDataSetChanged();
                    break;
                case 1:

                    List<TrainEntity> entities1 = null;

                    try{
                        entities1 = JSON.parseArray(msg.obj.toString(),TrainEntity.class);
                    }catch (Exception e){

                    }

                    if (entities1==null){
                        return;
                    }

                    List<String> imgs = new ArrayList<>();
                    List<String> titles = new ArrayList<>();


                    for (TrainEntity trainEntity : entities1) {
                        imgs.add(Url.imgUrl + trainEntity.getPic());
                        titles.add(trainEntity.getTrain_name());
                    }

                    break;
            }

        }
    }



}
